package com.sndscl.rpc.common.rpc;

import com.alibaba.fastjson.JSONObject;
import com.sndscl.rpc.common.dto.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.CharsetUtil;

import java.util.List;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/13 14:14
 **/
public class RequestDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int len = byteBuf.readableBytes();
        byte[] bytes = new byte[len];
        byteBuf.readBytes(bytes);
        String requestJson = new String(bytes, CharsetUtil.UTF_8);
        RpcRequest rpcRequest = JSONObject.parseObject(requestJson, RpcRequest.class);
        list.add(rpcRequest);
    }
}
